<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>仪表盘 - 物流信息管理系统</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            min-height: 100vh;
            background-color: #f8f9fa;
        }
        .navbar {
            position: fixed;
            top: 0;
            right: 0;
            left: 0;
            z-index: 1030;
            height: 60px;
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,.1);
        }
        .sidebar {
            position: fixed;
            top: 60px;
            bottom: 0;
            left: 0;
            z-index: 1020;
            width: 240px;
            background-color: #fff;
            box-shadow: 1px 0 4px rgba(0,0,0,.1);
            overflow-y: auto;
        }
        .sidebar-sticky {
            padding-top: 1rem;
        }
        .main-content {
            margin-left: 240px;
            padding: 80px 20px 20px;
            min-height: calc(100vh - 60px);
        }
        .nav-link {
            color: #333;
            padding: 10px 20px;
            display: block;
            border-radius: 4px;
            margin: 4px 8px;
        }
        .nav-link:hover {
            background-color: #f8f9fa;
            text-decoration: none;
        }
        .nav-link.active {
            background-color: #0d6efd;
            color: white;
        }
        .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        .card {
            margin-bottom: 20px;
            border: none;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            border-radius: 8px;
        }
        .stat-card {
            background: linear-gradient(45deg, #0d6efd, #0a58ca);
            color: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        .stat-card .icon {
            font-size: 2.5rem;
            opacity: 0.8;
        }
        .stat-card .number {
            font-size: 2rem;
            font-weight: bold;
            margin: 10px 0;
        }
        .stat-card .label {
            font-size: 1rem;
            opacity: 0.8;
        }
        .chart-container {
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0,0,0,.05);
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">物流信息管理系统</a>
            <div class="d-flex align-items-center">
                <span class="navbar-text me-3" id="userInfo">
                    欢迎，<span id="username"></span>
                </span>
                <button class="btn btn-outline-danger btn-sm" onclick="logout()">退出登录</button>
            </div>
        </div>
    </nav>

    <!-- 侧边栏 -->
    <nav class="sidebar">
        <div class="sidebar-sticky">
            <ul class="nav flex-column">
                <li class="nav-item">
                    <a class="nav-link active" href="/dashboard">
                        <i class="fas fa-tachometer-alt"></i>
                        仪表盘
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/order">
                        <i class="fas fa-box"></i>
                        订单管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/transport">
                        <i class="fas fa-truck"></i>
                        运输管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/warehouse">
                        <i class="fas fa-warehouse"></i>
                        仓库管理
                    </a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/storage">
                        <i class="fas fa-database"></i>
                        存储记录
                    </a>
                </li>
            </ul>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <main class="main-content">
        <div class="container-fluid">
            <!-- 统计卡片 -->
            <div class="row">
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="icon">
                            <i class="fas fa-box"></i>
                        </div>
                        <div class="number" id="totalOrders">0</div>
                        <div class="label">总订单数</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="icon">
                            <i class="fas fa-truck"></i>
                        </div>
                        <div class="number" id="totalTransports">0</div>
                        <div class="label">运输中订单</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="icon">
                            <i class="fas fa-warehouse"></i>
                        </div>
                        <div class="number" id="totalWarehouses">0</div>
                        <div class="label">仓库数量</div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="stat-card">
                        <div class="icon">
                            <i class="fas fa-database"></i>
                        </div>
                        <div class="number" id="totalStorages">0</div>
                        <div class="label">存储记录</div>
                    </div>
                </div>
            </div>

            <!-- 图表区域 -->
            <div class="row">
                <div class="col-md-6">
                    <div class="chart-container">
                        <h5 class="mb-4">订单状态分布</h5>
                        <canvas id="orderStatusChart"></canvas>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="chart-container">
                        <h5 class="mb-4">仓库使用情况</h5>
                        <canvas id="warehouseUsage"></canvas>
                    </div>
                </div>
            </div>

            <!-- 最近订单 -->
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">最近订单</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>订单号</th>
                                    <th>客户名称</th>
                                    <th>目的地</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                </tr>
                            </thead>
                            <tbody id="recentOrders"></tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/Chart.js/3.7.0/chart.min.js"></script>
    <script>
        $(function() {
            // 获取用户信息
            $.get('/api/user/info', function(res) {
                if (res.code === 200) {
                    $('#username').text(res.data.username);
                } else {
                    window.location.href = '/login';
                }
            });

            // 加载统计数据
            loadStatistics();
            // 加载图表
            loadCharts();
            // 加载最近订单
            loadRecentOrders();
        });

        function loadStatistics() {
            $.get('/api/dashboard/statistics', function(res) {
                if (res.code === 200) {
                    $('#totalOrders').text(res.data.totalOrders);
                    $('#totalTransports').text(res.data.totalTransports);
                    $('#totalWarehouses').text(res.data.totalWarehouses);
                    $('#totalStorages').text(res.data.totalStorages);
                }
            });
        }

        function loadCharts() {
            // 订单状态分布图表
            $.get('/api/dashboard/order-status', function(res) {
                if (res.code === 200) {
                    new Chart(document.getElementById('orderStatusChart'), {
                        type: 'pie',
                        data: {
                            labels: res.data.map(item => item.label),
                            datasets: [{
                                data: res.data.map(item => item.value),
                                backgroundColor: [
                                    '#0d6efd',
                                    '#198754',
                                    '#ffc107',
                                    '#dc3545'
                                ]
                            }]
                        },
                        options: {
                            responsive: true,
                            plugins: {
                                legend: {
                                    position: 'bottom'
                                }
                            }
                        }
                    });
                }
            });

            // 仓库使用情况图表
            $.get('/api/dashboard/warehouse-usage', function(res) {
                if (res.code === 200) {
                    new Chart(document.getElementById('warehouseUsage'), {
                        type: 'bar',
                        data: {
                            labels: res.data.map(item => item.name),
                            datasets: [{
                                label: '使用率',
                                data: res.data.map(item => item.usage),
                                backgroundColor: '#0d6efd'
                            }]
                        },
                        options: {
                            responsive: true,
                            scales: {
                                y: {
                                    beginAtZero: true,
                                    max: 100,
                                    ticks: {
                                        callback: function(value) {
                                            return value + '%';
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            });
        }

        function loadRecentOrders() {
            $.get('/api/dashboard/recent-orders', function(res) {
                if (res.code === 200) {
                    let html = '';
                    res.data.forEach(function(order) {
                        html += `
                            <tr>
                                <td>${order.orderNo}</td>
                                <td>${order.customerName}</td>
                                <td>${order.destination}</td>
                                <td>${getOrderStatusText(order.status)}</td>
                                <td>${order.createTime}</td>
                            </tr>
                        `;
                    });
                    $('#recentOrders').html(html);
                }
            });
        }

        function getOrderStatusText(status) {
            const statusMap = {
                0: '待处理',
                1: '运输中',
                2: '已完成',
                3: '已取消'
            };
            return statusMap[status] || '未知状态';
        }

        function logout() {
            $.post('/api/user/logout', function() {
                window.location.href = '/login';
            });
        }
    </script>
</body>
</html> 